--[[ The following commands were taken from InFlight, and will be modified a little ]]--
function KragHonor2:CreateBar() -- {{{
	-- Make a frame called "KragHonor2Bar"
	local frame = CreateFrame("Frame", "KragHonor2Bar", UIParent)

	-- Sane defaults
	if not self.db.profile.x then
		frame:SetPoint("CENTER", UIParent, "CENTER", 200, 200)
	else
		frame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", self.db.profile.x, self.db.profile.y)
	end

	-- Make it moveable and interfaceable with a mouse
	frame:SetMovable(true)
	frame:EnableMouse(true)
	frame:RegisterForDrag("LeftButton")

	-- Dragging
	frame:SetScript("OnDragStart", function() if IsShiftKeyDown() then this:StartMoving() end end)
	frame:SetScript("OnDragStop", function() 
		this:StopMovingOrSizing() 
		local a,b,c,d,e = this:GetPoint()
		if a == "TOPLEFT" and c == "TOPLEFT" then
			self.db.profile.x = floor(d + 0.5)
			self.db.profile.y = floor(e + 0.5)
		end
	end)

	-- MouesOver
	frame:SetScript("OnEnter", function()
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT")
		GameTooltip:SetText("KragHonor2")
		GameTooltip:AddLine(" Shift left-click and drag to move.")
		GameTooltip:AddLine(" Right-click for menu.")
		GameTooltip:Show()
	end)

	-- MouseOut
	frame:SetScript("OnLeave", function() GameTooltip:Hide() end)
	
	-- Bar
	frame.bar = CreateFrame("StatusBar", nil, frame)

	-- Spark
	frame.spark = frame.bar:CreateTexture(nil, "OVERLAY")

	-- Goal Name
	frame.goalName = frame.bar:CreateFontString(nil, "OVERLAY")

	-- Goal Number
	frame.diffText = frame.bar:CreateFontString(nil, "OVERLAY")
	
	-- Load the mouse menu and register it
	self:LoadOptions()
	self.dewdrop:Register(KragHonor2Bar, 'children', function() self.dewdrop:FeedAceOptionsTable(self.opts)	end, 'cursorX', true, 'cursorY', true );

	-- Update the Look and Data
	self:UpdateLook()
	self:UpdateHonorBar()
end
-- }}}
function KragHonor2:UpdateLook() -- {{{
	local db
	local f, _ = GameFontHighlightSmall:GetFont()
	local frame = KragHonor2Bar
	
	-- if available, use your oCB settings to customize the flight bar
	if oCB and self.db.profile.ocb then
		if self.db.profile.fill == "Deplete" then
			db = oCB.db.profile["MirrorBar"]
		else
			db = oCB.db.profile["CastingBar"]
		end
	end
	if db then
		--[[
		--	Frame
		--]]
		frame:SetWidth(db.width+9)
		frame:SetHeight(db.height+10)
		frame:SetBackdrop({
			bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", tile = true, tileSize = 16,
			--edgeFile = oCB.Borders[db.edgeFile], edgeSize = 16,
			--insets = {left = 5, right = 5, top = 5, bottom = 5},
		})

		-- Frame's Border Color
		if db.BorderColor then
			frame:SetBackdropBorderColor(db.BorderColor.r, db.BorderColor.g, db.BorderColor.b, 0.8)
		else
			frame:SetBackdropBorderColor(0, 0, 0, 0.75)
		end

		-- Frame's Fill Color
		if db.BackColor then
			frame:SetBackdropColor(db.BackColor.r, db.BackColor.g, db.BackColor.b, 0.4)
			frame.bar:SetStatusBarColor(db.BackColor.r, db.BackColor.g, db.BackColor.b, 0.4)
		else
			frame:SetBackdropColor(0, 0, 0, 0.5)
			frame.bar:SetStatusBarColor(0, 0, 0, 0.4)
		end
		
		--[[
		--	Actual Bar
		--]]
		frame.bar:ClearAllPoints()
		frame.bar:SetPoint("CENTER", frame, "CENTER", 0, 0)
		frame.bar:SetWidth(db.width)
		frame.bar:SetHeight(db.height)
		frame.bar:SetStatusBarTexture("Interface\\AddOns\\KragHonor2\\Textures\\glaze")
		
		--[[
		--	Spark
		--]]
		frame.spark:SetTexture("Interface\\CastingBar\\UI-CastingBar-Spark")
		frame.spark:SetWidth(16)
		frame.spark:SetHeight(db.height*2.44)
		frame.spark:SetBlendMode("ADD")
		
	else
		frame:SetWidth(239)
		frame:SetHeight(30)
		frame:SetBackdrop({
			bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", tile = true, tileSize = 16,
			--edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", edgeSize = 16,
			--insets = {left = 5, right = 5, top = 5, bottom = 5},
		})
		frame:SetBackdropBorderColor(0.8, 0.8, 0.8, 0.8)
		frame:SetBackdropColor(0.3, 0.3, 0.3, 0.4)
		
		frame.bar:ClearAllPoints()
		frame.bar:SetPoint("CENTER", frame, "CENTER", 0, 0)
		frame.bar:SetWidth(230)
		frame.bar:SetHeight(22)
		frame.bar:SetStatusBarTexture("Interface\\AddOns\\KragHonor2\\Textures\\glaze")
		
		frame.spark:SetTexture("Interface\\CastingBar\\UI-CastingBar-Spark")
		frame.spark:SetWidth(14)
		frame.spark:SetHeight(12*2.44)
		frame.spark:SetBlendMode("ADD")
	end
	
	frame.diffText:SetFont(f, 16)
	frame.diffText:ClearAllPoints()
	frame.goalName:SetFont(f, 12)
	frame.goalName:ClearAllPoints()
	
	if self.db.profile.inline then
		frame.diffText:SetJustifyH("RIGHT")
		frame.diffText:SetWidth(frame:GetWidth())
		frame.diffText:SetPoint("RIGHT", frame, "RIGHT", -6, 0)

		frame.goalName:SetJustifyH("LEFT")
		frame.goalName:SetJustifyV("CENTER")
		frame.goalName:SetWidth(frame:GetWidth()*0.7)
		frame.goalName:SetPoint("LEFT", frame, "LEFT", 6, 0)	
	else
		frame.diffText:SetJustifyH("CENTER")
		frame.diffText:SetWidth(frame:GetWidth())
		frame.diffText:SetPoint("CENTER", frame, "CENTER", 0, 0)

		frame.goalName:SetJustifyH("CENTER")
		frame.goalName:SetWidth(max(200, frame:GetWidth()*1.2))
		frame.goalName:SetPoint("BOTTOM", frame, "TOP", 0, 0)
	end	
end
-- }}}
function KragHonor2:UpdateHonorBar( runComputeHonor ) -- {{{
	-- Scrub Vars
	local f = KragHonor2Bar;
	local db = self.db.profile;

	-- Make sure we don't create an infinate loop
	if runComputeHonor then
		self:computeHonor( false );
	end

	-- Incase the goal text ran off... Reset it
	if (not f.goalName:GetText()) or (f.goalName:GetText() == "") then
		f.goalName:SetText(db.marks.name);
	end

	if db.diff >= 0 then
		-- If we are needing honor...
		f.diffText:SetText ( self:formatNumber ( db.diff ) )
		--f.bar:SetStatusBarColor ( db.color.r, db.color.g, db.color.b, 0.7 );
		--f.bar:SetMinMaxValues ( 0, 100 );
		f.bar:SetValue ( db.honorPercent );
		f.spark:Show();
	else
		-- If we are over the goal...
		f.diffText:SetText ( "+" .. self:formatNumber( math.abs( db.diff ) ) );
		f.bar:SetStatusBarColor ( 0, 1, 0, 0.7 );
		--f.bar:SetMinMaxValues ( 0, 100 );
		f.bar:SetValue ( 100 );
		f.spark:Hide();
	end
end
-- }}}
function KragHonor2:LoadOptions() -- {{{
	if self.optionsloaded then return end
	local db = self.db.profile;
	--local adb = self.db.profile.armorSet;
	--local wdb = self.db.profile.weaponSet;
	local adb = {};
	local wdb = {};

	self.opts = {
		type = "group",
		args = {
			header = {
				type = "header",
				name = "KragHonor2",
				order = 1,
			},
			frame = {
				type = "group",
				name = "Appearance",
				desc = "Change the way the statusbar look and behave.",
				order = 2,
				args = {
					oCB = {
						type = "toggle",
						order = 1,
						name = "Use oCB settings",
						desc = "Use the settings of oCB if available (will override local settings).",
						set = function(v) db.ocb = v self:UpdateLook() end,
						get = function() return db.ocb end,
					},
					inline = {
						type = "toggle",
						order = 2,
						name = "Inline mode",
						desc = "Shows only destination/time inside the statusbar",
						set = function(v) db.inline = v self:UpdateLook() 
							if not self.destination then return end
							if db.inline then
								KragHonor2Bar.goalName:SetText(self.destination)
							else
								KragHonor2Bar.goalName:SetText(self.source .. " --> " .. self.destination)
							end
						end,
						get = function() return db.inline end,
					},
					barcolor = {
						type = "color",
						order = 4,
						name = "Bar color",
						desc = "Set the color of the fill bar.",
						get = function()
							local v = db.color
							return v.r,v.g,v.b
						end,
						set = function(r,g,b) 
							db.color = {r=r, g=g, b=b} 
							if not db.ocb then KragHonor2Bar.bar:SetStatusBarColor(r, g, b) end
						end,
					}
				},
			},
			--[[
			gearFrame = {
				type = "group",
				name = "Item Goals",
				desc = "Goals for specific PvP Rewards",
				order = 3,
				args = {
					epicarmor = {
						type = "group",
						name = "Epic PvP Armor",
						desc = "Epic PvP Armor Sets",
						order = 2,
						args = {
							eboots = {
								type = "execute",
								order = 1,
								name = adb.epicBoots,
								desc = adb.epicBoots .. "\nBoots\n12,375 Honor\n20 Arathi Basin",
								func = function (v) self:setGoal( 12375, { name = self:getItemLink( adb.epicBoots ), type = 'AB', num = 20 } ) end,
							},
							echest = {
								type = "execute",
								order = 2,
								name = adb.epicChest,
								desc = adb.epicChest .. "\nChest\n20,250 Honor\n30 Arathi Basin",
								func = function (v) self:setGoal( 20250, { name = self:getItemLink( adb.epicChest ), type = 'AB', num = 30 } ) end,
							},
							egloves = {
								type = "execute",
								order = 3,
								name = adb.epicGloves,
								desc = adb.epicGloves .. "\nGloves\n12,375 Honor\n20 Alterac Valley",
								func = function (v) self:setGoal( 12375, { name = self:getItemLink( adb.epicGloves ), type = "AV", num = 20 } ) end,
							},
							ehelm = {
								type = "execute",
								order = 4,
								name = adb.epicHelm,
								desc = adb.epicHelm .. "\nHelm\n19,125 Honor\n30 Alterac Valley",
								func = function (v) self:setGoal( 19125, { name = self:getItemLink( adb.epicHelm ), type = "AV", num = 30 } ) end,
							},
							elegs = {
								type = "execute",
								order = 4,
								name = adb.epicLegs,
								desc = adb.epicLegs .. "\nLegs\n19,125 Honor\n30 Warsong Gulch",
								func = function (v) self:setGoal( 19125, { name = self:getItemLink( adb.epicLegs ), type = "WSG", num = 30 } ) end,
							},
							eshoulder = {
								type = "execute",
								order = 4,
								name = adb.epicShoulders,
								desc = adb.epicShoulders .. "\nShoulders\n12,375 Honor\n20 Arathi Basin",
								func = function (v) self:setGoal( 19125, { name = self:getItemLink( adb.epicShoulders ), type = "AB", num = 20 } ) end,
							},

						},
					},
					rarearmor = {
						type = "group",
						name = "Rare PvP Armor",
						desc = "Rare PvP Armor Sets",
						order = 4,
						args = {
							rboots = {
								type = "execute",
								order = 1,
								name = adb.rareBoots,
								desc = adb.rareBoots .. "\nBoots\n4,125 Honor\n20 Arathi Basin",
								func = function (v) self:setGoal( 4125, { name = self:getItemLink( adb.rareBoots ), type = 'AB', num = 20 } ) end,
							},
							rchest = {
								type = "execute",
								order = 2,
								name = adb.rareChest,
								desc = adb.rareChest .. "\nChest\n6,750 Honor\n30 Arathi Basin",
								func = function (v) self:setGoal( 6750, { name = self:getItemLink( adb.rareChest ), type = 'AB', num = 30 } ) end,
							},
							rgloves = {
								type = "execute",
								order = 3,
								name = adb.rareGloves,
								desc = adb.rareGloves .. "\nGloves\n4,125 Honor\n20 Alterac Valley",
								func = function (v) self:setGoal( 4125, { name = self:getItemLink( adb.rareGloves ), type = "AV", num = 20 } ) end,
							},
							rhelm = {
								type = "execute",
								order = 4,
								name = adb.rareHelm,
								desc = adb.rareHelm .. "\nHelm\n6,375 Honor\n30 Alterac Valley",
								func = function (v) self:setGoal( 6375, { name = self:getItemLink( adb.rareHelm ), type = "AV", num = 30 } ) end,
							},
							rlegs = {
								type = "execute",
								order = 5,
								name = adb.rareLegs,
								desc = adb.rareLegs .. "\nLegs\n6,375 Honor\n30 Warsong Gulch",
								func = function (v) self:setGoal( 6375, { name = self:getItemLink( adb.rareLegs ), type = "WSG", num = 30 } ) end,
							},
							rshoulder = {
								type = "execute",
								order = 6,
								name = adb.rareShoulders,
								desc = adb.rareShoulders .. "\nShoulders\n4,125 Honor\n20 Arathi Basin",
								func = function (v) self:setGoal( 6375, { name = self:getItemLink( adb.rareShoulders ), type = "AB", num = 20 } ) end,
							},

						},	
					},
					weapons = {
						type = "group",
						name = "Weapons",
						desc = "Weapons",
						order = 5,
						args = {
							twoHHeader = {
								type = "header",
								name = "Two-Handed Weapons",
								order = 1,
							},
							twoHSword = {
								type = "execute",
								order = 2,
								name = wdb.twoHSword,
								desc = wdb.twoHSword .. "\n2-Hand Sword\n45,000 Honor\n40 Alterac Valley",
								func = function () self:setGoal( 45000, { name = self:getItemLink( wdb.twoHSword ), type = "AV", num = 40 } ) end,
							},
							twoHAxe = {
								type = "execute",
								order = 3,
								name = wdb.twoHAxe,
								desc = wdb.twoHAxe .. "\n2-Hand Axe\n45,000 Honor\n40 Alterac Valley",
								func = function () self:setGoal( 45000, { name = self:getItemLink( wdb.twoHAxe ), type = "AV", num = 40 } ) end,
							},
							twoHMaceDPS = {
								type = "execute",
								order = 4,
								name = wdb.twoHMaceDPS,
								desc = wdb.twoHMaceDPS .. "\n2-Hand Mace (DPS)\n45,000 Honor\n40 Alterac Valley",
								func = function () self:setGoal( 45000, { name = self:getItemLink( wdb.twoHMaceDPS ), type = "AV", num = 40 } ) end,
							},
							twoHMaceCaster = {
								type = "execute",
								order = 5,
								name = wdb.twoHMaceCaster,
								desc = wdb.twoHMaceCaster .. "\n2-Hand Mace (Caster)\n45,000 Honor\n40 Alterac Valley",
								func = function () self:setGoal( 45000, { name = self:getItemLink( wdb.twoHMaceCaster ), type = "AV", num = 40 } ) end,
							},
							polearm = {
								type = "execute",
								order = 6,
								name = wdb.polearm,
								desc = wdb.polearm .. "\nPolearm\n45,000 Honor\n40 Alterac Valley",
								func = function () self:setGoal( 45000, { name = self:getItemLink( wdb.polearm ), type = "AV", num = 40 } ) end,
							},
							oneHAxe = {
								type = "execute",
								order = 7,
								name = wdb.staff,
								desc = wdb.staff .. "\nStave\n45,000 Honor\n40 Alterac Valley",
								func = function () self:setGoal( 45000, { name = self:getItemLink( wdb.staff ), type = "AV", num = 40 } ) end,
							},

							oneHHeader = {
								type = "header",
								name = "One-Handed Weapons",
								order = 8,
							},
							oneHAxe = {
								type = "execute",
								order = 9,
								name = wdb.oneHAxe,
								desc = wdb.oneHAxe .. "\n1-Hand Axe\n22,500 Honor\n20 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.oneHAxe ), type = "AB", num = 20 } ) end,
							},
							oneHSwordOne = {
								type = "execute",
								order = 10,
								name = wdb.oneHSwordOne,
								desc = wdb.oneHSwordOne .. "\n1-Hand Sword\n22,500 Honor\n20 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.oneHSwordOne ), type = "AB", num = 20 } ) end,
							},
							oneHSwordTwo = {
								type = "execute",
								order = 11,
								name = wdb.oneHSwordTwo,
								desc = wdb.oneHSwordTwo .. "\n1-Hand Sword\n22,500 Honor\n20 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.oneHSwordTwo ), type = "AB", num = 20 } ) end,
							},
							oneHMaceDPS = {
								type = "execute",
								order = 12,
								name = wdb.oneHMaceDPS,
								desc = wdb.oneHMaceDPS .. "\n1-Hand Mace (DPS)\n22,500 Honor\n20 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.oneHMaceDPS ), type = "AB", num = 20 } ) end,
							},
							oneHMaceCaster = {
								type = "execute",
								order = 13,
								name = wdb.oneHMaceCaster,
								desc = wdb.oneHMaceCaster .. "\n1-Hand Mace (Caster)\n22,500 Honor\n20 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.oneHMaceCaster ), type = "AB", num = 20 } ) end,
							},
							fistRight = {
								type = "execute",
								order = 14,
								name = wdb.fistRight,
								desc = wdb.fistRight .. "\nFist (Right)\n22,500 Honor\n20 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.fistRight ), type = "AB", num = 20 } ) end,
							},
							fistLeft = {
								type = "execute",
								order = 15,
								name = wdb.fistLeft,
								desc = wdb.fistLeft .. "\nFist (Left)\n22,500 Honor\n20 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.fistLeft ), type = "AB", num = 20 } ) end,
							},
							daggerDPS = {
								type = "execute",
								order = 16,
								name = wdb.daggerDPS,
								desc = wdb.daggerDPS .. "\n1-Hand Dagger (DPS)\n22,500 Honor\n20 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.daggerDPS ), type = "AB", num = 20 } ) end,
							},
							daggerCaster = {
								type = "execute",
								order = 17,
								name = wdb.daggerCaster,
								desc = wdb.daggerCaster .. "\n1-Hand Dagger (Caster)\n22,500 Honor\n20 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.daggerCaster ), type = "AB", num = 20 } ) end,
							},

							rangedHeader = {
								type = "header",
								order = 19,
								name = "Ranged Weapons",
							},
							bow = {
								type = "execute",
								order = 20,
								name = wdb.bow,
								desc = wdb.bow .. "\nBow\n22,500 Honor\n40 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.bow ), type = "AB", num = 40 } ) end,
							},
							crossbow = {
								type = "execute",
								order = 21,
								name = wdb.crossbow,
								desc = wdb.crossbow .. "\nCrossbow\n22,500 Honor\n40 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.crossbow ), type = "AB", num = 40 } ) end,
							},
							gun = {
								type = "execute",
								order = 22,
								name = wdb.gun,
								desc = wdb.gun .. "\nGun\n22,500 Honor\n40 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.gun ), type = "AB", num = 20 } ) end,
							},

							offHandHeader = {
								type = "header",
								order = 23,
								name = "Off-Hands",
							},
							shield = {
								type = "execute",
								order = 24,
								name = wdb.shield,
								desc = wdb.shield .. "\nShield\n22,500 Honor\n20 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.shield ), type = "AB", num = 20 } ) end,
							},
							tomeDamage = {
								type = "execute",
								order = 25,
								name = wdb.tomeDamage,
								desc = wdb.tomeDamage .. "\nOff-Hand [Book] (Damage)\n22,500 Honor\n40 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.tomeDamage ), type = "AB", num = 40 } ) end,
							},
							tomeHealing = {
								type = "execute",
								order = 26,
								name = wdb.tomeHealing,
								desc = wdb.tomeHealing .. "\nOff-Hand [Book] (Healing)\n22,500 Honor\n40 Arathi Basin",
								func = function () self:setGoal( 22500, { name = self:getItemLink( wdb.tomeHealing ), type = "AB", num = 40 } ) end,
							},
						},
					},
					supplies = {
						type = "group",
						name = "Officer Supplies",
						desc = "Pots, Tabard, Battle Standard, and more!",
						order = 3,
						args = {
							SuppliesTempHeader = {
								type = "header",
								name = "Comin' Soon!",
								desc = "This section is still being worked on.  Hang in there!",
							},
						},
					},
				},
			},
			--]]
		},
	}
	self.dewdrop = AceLibrary("Dewdrop-2.0")
	self.optionsloaded = true
end
-- }}}
